<?php
/**
 * Create by chang
 * Date 2019/12/20
 * Time 18:22
 * 代言人
 */
namespace app\common\service;

use app\common\model\Promotion;
use app\common\model\User;
use think\Db;

class PromotionServer extends Server {
	
	
	/**
	 * 添加代言人资料
	 * @param $user
	 * @return array
	 */
	public function createInfo($user) {
		$agentId = '';
		$area = $user['area'];
		if($user['recommend_user_id']) {
			$recommendUser = User::get($user['recommend_user_id']);
			$where = ['user_id' => $user['recommend_user_id']];
			if($recommendUser['level'] == \app\admin\model\User::LEVEL_PROMOTION) {
				$promotion = Promotion::get(['user_id' => $user['recommend_user_id']]);
				if($promotion && $promotion['agent_shop_id']) {
					$where = ['id' => $promotion['agent_shop_id']];
				}
			}
			$agentShop= Db::name('agentShop')->where($where)->find();
			if($agentShop) {
				$agentId = $agentShop['id'];
				$area = $agentShop['area'];
			}
		}
		
		
		$model = new Promotion();
		$data = [
			'user_id' => $user['id'],
			'agent_shop_id' => $agentId,
			'name' =>  $user['nickname'],
			'status' => $model::STATUS_Y,
			'area' => $area,
			'recommend_user_id' => $user['recommend_user_id'] ? : '',
			'commission_status' => 1
		];
		$res = $model->allowField(true)->isUpdate(false)->save($data);
		
		
		if($res !== false) {
			return $this->success();
		}	 else {
			return $this->error('添加资料出错');
		}
	}
	
}
